<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="no.ntnu.tdt4237.*" %>
    <%@ page import="no.ntnu.tdt4237.helperactions.*" %>
    <%@page import="java.util.regex.*"%>
    <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<tags:header title=" - Profile"></tags:header>

<body>
<%
User loggedInUser = (User) session.getAttribute(SessionKeys.USER_OBJECT);
%>
<tags:menu loggedInUser="<%=loggedInUser%>"></tags:menu>
<%
String errormessage = "";
boolean registerSuccess = false;
boolean register = request.getParameter("Password") !=null; 

if (register && loggedInUser != null) {
	//Set the email pattern string
		Pattern p = Pattern.compile("\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b");
		//Match the given string with the pattern
		Matcher m = p.matcher(request.getParameter("Email"));
		boolean flawless = true;

		if (Database.getUser(request.getParameter("UserName")) != null && !request.getParameter("UserName").equals(loggedInUser.getUserName())){
			// The user doesn't exist
			errormessage += "<p>Username already in use!<p/>";
			flawless = false;
		}
		if(request.getParameter("UserName").length() < 4 ){
			errormessage += "<p>Username length must be 4 characters or longer</p>";
			flawless = false;
		} 
		if(request.getParameter("Password").length() < 6 ){
			errormessage += "<p>Password length must be 6 characters or longer</p>";
			flawless = false;
		} 
		if(request.getParameter("FirstName").length() < 2 ){
			errormessage += "<p>First name must be 2 letters or longer</p>";
			flawless = false;
		} 
		if(request.getParameter("LastName").length() < 2 ){
			errormessage += "<p>Last name must be 2 letters or longer</p>";
			flawless = false;
		} 
		if(!m.matches()){
			errormessage += "<p>Not a valid email address<p/>";
			flawless = false;
		}
		
		if(flawless) {
			System.out.println(loggedInUser.getUserID());
			registerSuccess = Database.saveUser(new User(loggedInUser.getUserID(), request.getParameter("UserName"), 
					   request.getParameter("Password"), 
					   request.getParameter("Email"),
					   request.getParameter("FirstName"),
					   request.getParameter("LastName")));
		}
		else {
			%>
			<tags:error errorMsg="<%=errormessage %>"></tags:error>
			<%			
		}
	

}


if (registerSuccess) {
	loggedInUser.setEmail(request.getParameter("Email"));
	loggedInUser.setFirstName(request.getParameter("FirstName"));
	loggedInUser.setLastName(request.getParameter("LastName"));
	loggedInUser.setPassword(request.getParameter("Password"));
	loggedInUser.setUserName(request.getParameter("UserName"));
	%>
		<tags:message message="Changes registered!"></tags:message>
	<%
}
else
{
	String blogOwnerName = StringHelpers.getProfileOwnerName(request.getRequestURL().toString());
	User profileUser = Database.getUser(blogOwnerName);
	if(loggedInUser != null && blogOwnerName.equals(loggedInUser.getUserName()))
	{
	%>
	<div class="ProfileBox">
		<form action="" method="post">
			<span>First Name:</span><input type="text" name="FirstName" value="<%=loggedInUser.getFirstName() %>"></input><br/>
			<span>Last Name:</span><input type="text" name="LastName" value="<%=loggedInUser.getLastName() %>"></input><br/>
			<span>Email address:</span><input type="text" name="Email" value="<%=loggedInUser.getEmail() %>"></input><br/>
			<span>User Name:</span><input type="text" name="UserName" value="<%=loggedInUser.getUserName() %>"></input><br/>
			<span>Password:</span><input type="password" name="Password"></input><br/>
			<input type="submit" value="Update"></input>
		</form>
	</div>
	<%
	}

	else
	{
		%>
		<tags:error errorMsg="You are not allowed to see this page!"></tags:error>
		<%
	}
}
%>
</body>
</html>